import { createVNode, getCurrentInstance, render, nextTick } from 'vue';
import FormModalConstructor from './formModal';

export function useFormModal() {
  async function getModalInstance(props) {
    await nextTick();
    const appContext = getCurrentInstance()?.appContext;
    const container = document.createElement('div');
    const vNode = createVNode(FormModalConstructor, props);
    vNode.appContext = appContext;
    render(vNode, container);
    document.body.append(container);
    props.onVanish = () => {
      render(null, container);
      document.body.removeChild(container);
    };
    return vNode;
  }

  function show(props) {
    getModalInstance(props);
  }

  return [
    {
      show,
    },
  ];
}
